#include "mainwindow.h"

#include "editorarea.h"

#include <QtCore/QSettings>
#include <QtGui/QApplication>
#include <QtGui/QCloseEvent>
#include <QtGui/QDesktopWidget>
#include <QtGui/QDockWidget>
#include <QtGui/QMenuBar>
#include <QtGui/QToolBar>

MainWindow* mainWindow = 0;

MainWindow* MainWindow::getInstance()
{
    if (mainWindow == 0)
    {
        mainWindow = new MainWindow();
        mainWindow->init();
    }

    return mainWindow;
}

void MainWindow::destroy()
{
    if (mainWindow != 0)
    {
        delete mainWindow;
        mainWindow = 0;
    }
}

QMenu* MainWindow::getMenu(const QString& menuId) const
{
    QMenu* ret = 0;

    if (m_menus.contains(menuId))
    {
        ret = m_menus[menuId];
    }

    return ret;
}

QToolBar* MainWindow::getToolBar(const QString& toolBarId) const
{
    QToolBar* ret = 0;

    if (m_toolBars.contains(toolBarId))
    {
        ret = m_toolBars[toolBarId];
    }

    return ret;
}

void MainWindow::putToBag(const QString& key, const QVariant& value)
{
    m_bag[key] = value;
}

QVariant MainWindow::bagValue(const QString& key) const
{
    QVariant ret;

    if (m_bag.contains(key))
    {
        ret = m_bag[key];
    }

    return ret;
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    if (event->isAccepted())
    {
        writeSettings();

        close();
    }
}

MainWindow::MainWindow() : QMainWindow(),
    m_bag(),
    m_menus(),
    m_toolBars(),
    m_editorArea(),
    m_fileMenu(0),
    m_editMenu(0),
    m_viewMenu(0),
    m_toolsMenu(0),
    m_helpMenu(0),
    m_undoToolbar(0),
    m_undoAction(0),
    m_redoAction(0),
    m_undoMenuAction(0),
    m_redoMenuAction(0),
    m_restoreSessionMenuAction(0),
    m_optionsMenuAction(0),
    m_aboutMenuAction(0)
{
    const QDesktopWidget* desktop = qApp->desktop();
    const QRect screenSize = desktop->availableGeometry(
        desktop->primaryScreen());
    const int width = screenSize.width() / 2;
    const int height = screenSize.height() - 150;

    move((screenSize.width() - width) / 2,
        (screenSize.height() - height) / 2);
    resize(width, height);
}

MainWindow::~MainWindow()
{

}

void MainWindow::init()
{
    readSettings();

    createActions();
    createMenus();
    addMenus();
    createToolbars();
    addToolBars();

    connectSlots();

    setCentralWidget(m_editorArea);
    setWindowTitle(tr("CPN Script compiler"));
}

void MainWindow::addMenus()
{
    m_menus[FILE_MENU] = m_fileMenu;
    m_menus[EDIT_MENU] = m_editMenu;
    m_menus[VIEW_MENU] = m_viewMenu;
    m_menus[TOOLS_MENU] = m_toolsMenu;
    m_menus[HELP_MENU] = m_helpMenu;
    m_menus[FINISHED] = 0;
}

void MainWindow::addToolBars()
{

}

void MainWindow::createActions()
{
    m_undoAction = new QAction(tr("&Undo"), this);

    m_redoAction = new QAction(tr("&Redo"), this);

    m_undoMenuAction = new QAction(tr("&Undo"), this);
    m_undoMenuAction->setShortcuts(QKeySequence::Undo);
    m_undoMenuAction->setShortcutContext(Qt::WindowShortcut);

    m_redoMenuAction = new QAction(tr("&Redo"), this);
    m_redoMenuAction->setShortcuts(QKeySequence::Redo);
    m_redoMenuAction->setShortcutContext(Qt::WindowShortcut);

    m_restoreSessionMenuAction = new QAction(tr("&Restore session"), this);

    m_optionsMenuAction = new QAction(tr("&Options"), this);

    m_aboutMenuAction = new QAction(tr("&About"), this);
}

void MainWindow::createMenus()
{
    m_fileMenu = menuBar()->addMenu(tr("&FILE"));

    m_editMenu = menuBar()->addMenu(tr("&EDIT"));
    m_editMenu->addAction(m_undoMenuAction);
    m_editMenu->addAction(m_redoMenuAction);

    m_viewMenu = menuBar()->addMenu(tr("&VIEW"));

    m_toolsMenu = menuBar()->addMenu(tr("&TOOLS"));
    m_toolsMenu->addAction(m_restoreSessionMenuAction);
    m_toolsMenu->addAction(m_optionsMenuAction);

    m_helpMenu = menuBar()->addMenu(tr("&HELP"));
    m_helpMenu->addAction(m_aboutMenuAction);
}

void MainWindow::createToolbars()
{
    m_undoToolbar = addToolBar(tr("Undo"));
    m_undoToolbar->setObjectName("UndoToolbar");
    m_undoToolbar->addAction(m_undoAction);
    m_undoToolbar->addAction(m_redoAction);
}

void MainWindow::createStatusBar()
{
    // do nothing
}

void MainWindow::connectSlots()
{
}

void MainWindow::readSettings()
{
    QSettings appSettings(QSettings::NativeFormat, QSettings::UserScope,
        "VSB", "CPNS");
}

void MainWindow::writeSettings()
{
    QSettings appSettings(QSettings::NativeFormat, QSettings::UserScope,
        "VSB", "CPNS");

    if (!isMinimized() && !isMaximized())
    {
        appSettings.beginGroup("MainWindow");
        appSettings.setValue("Pos", pos());
        appSettings.setValue("Size", size());
        appSettings.endGroup();
    }

    appSettings.beginGroup("MainWindowState");
    appSettings.setValue("state", saveState());
    appSettings.endGroup();
}
